import cpy from 'cpy';
import chalk from "chalk";
import {un7z} from "./utils/un7z.js";
import config from "../long.config.js";
import {pathJoin} from "./utils/pathUtils.js";

const {builderCacheList, electronCache} = config

if (builderCacheList && builderCacheList.length > 0) {
    builderCacheList.map(item => {
        const {name, version, from, to} = item
        cpy(from, to).on('progress', async progress => {
            // const { completedFiles,totalFiles,percent } = progress

            if (progress.percent === 1) {
                console.log(chalk.greenBright(`${name}拷贝完成,`), chalk.magenta(`版本号为${version}`))

                const pkgNameVersion = `${name}-${version}`
                const targetPath = pathJoin(to, pkgNameVersion)
                const file = pathJoin(to, `${name}-${version}` + '.7z')

                await un7z(file, targetPath).then(() => {
                    console.log(chalk.blue(`${pkgNameVersion}解压完成~`))
                })
            }
        });
    })
}

/**
 * 只需要拷贝electron包即可，不需要解压
 */
if (electronCache) {
    const {name, version, from, to} = electronCache

    cpy(from, to).on('progress', async progress => {
        if (progress.percent === 1) {
            console.log(chalk.greenBright(`${name}拷贝完成,`), chalk.magenta(`版本号为${version}`))
        }
    })
}
